package com.example.dellpc.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hb.views.PinnedSectionListView;

import java.util.List;

import static com.example.dellpc.myapplication.MyAdapter.*;


/**
 * Created by dellpc on 2016/8/15.
 */
public class MyPinnedSectionListViewAdapter extends BaseAdapter implements PinnedSectionListView.PinnedSectionListAdapter {

    private Context context;
    private List<PinnedSectionListViewActivity.Item> items;

    public MyPinnedSectionListViewAdapter(List<PinnedSectionListViewActivity.Item> items, Context context) {
        this.items = items;
        this.context = context;
    }

    @Override
    public boolean isItemViewTypePinned(int viewType) {
        return viewType == PinnedSectionListViewActivity.Item.TITLE;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return items.get(position).type;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH vh = null;

        if (items.get(position).type == PinnedSectionListViewActivity.Item.TITLE) {
            if (convertView == null) {
                vh=new VH();
                convertView = LayoutInflater.from(context).inflate(R.layout.title_item, parent, false);
                vh.textView = (TextView) convertView.findViewById(R.id.tv_title);
                convertView.setTag(vh);

            } else {
                vh = (VH) convertView.getTag();
            }
            vh.textView.setText(items.get(position).text);

        } else if (items.get(position).type == PinnedSectionListViewActivity.Item.ITEM) {
            if (convertView == null) {
                vh=new VH();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_item, parent, false);
                vh.textView = (TextView) convertView.findViewById(R.id.tv_item);
                convertView.setTag(vh);
            } else {
                vh = (VH) convertView.getTag();
            }
            vh.textView.setText(items.get(position).text);
        }

        return convertView;
    }


    public class VH {

        public TextView textView;
    }
}
